package com.example.conference.mapper;

import com.example.conference.domain.CourseStudent;
import com.example.conference.domain.User;
import com.example.conference.domain.dto.PageParam;
import com.example.conference.domain.vo.UserVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from user")
    Page<User> queryAllPagination();


    @Select("select * from user where role_id = #{roleId}")
    @ResultMap("UserMap")
    Page<User> queryByRole(Integer roleId);

    @Select("select * from user where role_id = #{roleId} and dept_id = #{deptId}")
    List<User> queryUserByRoleAndDept(Integer roleId, Integer deptId);

    @Select("select * from user where user_id = #{userId} and password = #{password}")
    Integer checklogin(User u);

    @Select("select * from user where user_id = #{userId}")
    @ResultMap("UserMap")
    UserVo queryByIdToVo(Integer userId);

    @Select("select * from user where user_id = #{userId}")
    User queryById(Integer userId);


    @Insert("insert into user VALUES (#{userId},#{username},#{password},#{roleId},#{deptId},#{majorId},#{userAvatar})")
    Integer add(User user);

    @Update("update user Set username = #{username},role_id = #{roleId},dept_id = #{deptId},major_id = #{majorId} where user_id = #{userId}")
    Integer update(User user);

    @Delete("delete from user where user_id = #{userId}")
    Integer delete(Integer userId);

    @Select("SELECT * FROM user")
    @Results(id= "UserMap", value={
            @Result(column="role_id", property="role" ,
                    one=@One(select="com.example.conference.mapper.RoleMapper.queryById")),
            @Result(column="dept_id", property="dept" ,
                    one=@One(select="com.example.conference.mapper.DeptMapper.queryById")),
            @Result(column="major_id", property="major" ,
                    one=@One(select="com.example.conference.mapper.MajorMapper.queryById"))
            })
    Page<UserVo> queryToUserMap();


    @Select("SELECT * FROM user where username like CONCAT('%', #{username},'%')")
    @ResultMap("UserMap")
    Page<UserVo> queryByName(String username);


}
